﻿using Basic.OACollect;
using Basic.OAExtendService;
using Basic.OAModel.DB;
using Basic.OARemoteModel.UserFill.Model;
using Basic.OAService.Interface;
using WeDonekRpc.Client;
using WeDonekRpc.Helper;
using WeDonekRpc.Model;

namespace Basic.OAService.lmpl
{
    internal class UserFillService : IUserFillService
    {
        private const string _FormLinkType = "fillForm";
        private readonly IEmpFillInItemCollect _FillInItem;
        private readonly IFormUserRecordService _UserRecord;
        private readonly IDataFillInCollect _DataFillIn;

        public UserFillService ( IEmpFillInItemCollect fillInItem,
            IFormUserRecordService userRecord,
            IDataFillInCollect dataFillIn )
        {
            this._FillInItem = fillInItem;
            this._UserRecord = userRecord;
            this._DataFillIn = dataFillIn;
        }

        public UserFillData GetItem ( long id )
        {
            return this._FillInItem.GetItem(id);
        }
        public PagingResult<UserFillItem> Query ( UserFillItemQuery query, IBasicPage paging )
        {
            UserFillItem[] list = this._FillInItem.QueryFillItem(query, paging, out int count);
            return new PagingResult<UserFillItem>(list, count);
        }
        public bool Revoke ( long id, long empId )
        {
            DBEmpFillInItem item = this._FillInItem.Get(id);
            if ( item.EmpId != empId )
            {
                throw new ErrorException("oa.fill.item.no.power");
            }
            return this._FillInItem.Revoke(item);
        }
        public bool Submit ( long id, long empId )
        {
            DBEmpFillInItem item = this._FillInItem.Get(id);
            if ( item.EmpId != empId )
            {
                throw new ErrorException("oa.fill.item.no.power");
            }
            long formId = this._DataFillIn.Get(item.FillInId, a => a.TableId);
            if ( this._UserRecord.CheckIsNull(formId, id, _FormLinkType) )
            {
                throw new ErrorException("oa.fill.user.record.null");
            }
            return this._FillInItem.Submit(item);
        }
    }
}
